package co.yixiang.modules.shop.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.VipArticle;
import co.yixiang.modules.shop.service.VipArticleService;
import co.yixiang.modules.shop.service.dto.VipArticleQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-09
*/
@Api(tags = "资料库管理")
@RestController
@RequestMapping("/api/vipArticle")
public class VipArticleController {

    private final VipArticleService vipArticleService;

    public VipArticleController(VipArticleService vipArticleService) {
        this.vipArticleService = vipArticleService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('vipArticle:list')")
    public void download(HttpServletResponse response, VipArticleQueryCriteria criteria) throws IOException {
        vipArticleService.download(vipArticleService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询资料库")
    @ApiOperation("查询资料库")
//    @AnonymousAccess
    @PreAuthorize("@el.check('vipArticle:list')")
    public ResponseEntity<Object> getVipArticles(VipArticleQueryCriteria criteria, Pageable pageable){
        HashMap<String, Object> resultMap = new HashMap<>();
        Map<String, Object> map = vipArticleService.queryAll(criteria, pageable);
        resultMap.put("code",200);
        resultMap.put("message",map);
        return new ResponseEntity<>(resultMap,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增资料库")
    @ApiOperation("新增资料库")
    @PreAuthorize("@el.check('vipArticle:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody VipArticle resources){

        String suffix=resources.getWordsName();
        String []suffixs=suffix.split(".");
        resources.setWordsSuffix(suffixs[suffixs.length-1]);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message",vipArticleService.create(resources));
        return new ResponseEntity<>(resultMap,HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改资料库")
    @ApiOperation("修改资料库")
    @PreAuthorize("@el.check('vipArticle:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody VipArticle resources){
        vipArticleService.update(resources);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        return ResponseEntity.ok(resultMap);
    }

    @Log("删除资料库")
    @ApiOperation("删除资料库")
    @PreAuthorize("@el.check('vipArticle:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        vipArticleService.deleteAll(ids);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        return ResponseEntity.ok(resultMap);
    }
}